#include "GAF_Application.h"
#include "GAF_Timer.h"
#include "GAF_OutPut.h"

namespace GAF
{
	Application* Application::ThePApp = NULL;

	void Application::DeleteAllResource()
	{
		// 清除所有图片资源
		Image::DestroyAll();

		// 清除所有资源包
		Package::DestroyAll();
	}

	void Application::DeleteAllMemory()
	{
		// 清除未释放的内存
		if(GAF_MEM_SIZE() > 0)
		{
#ifdef GAF_DEBUG
			STD_COUT << "The memory has " << GAF_MEM_COUNT() << " handles not release." << STD_ENDL;
			STD_COUT << "Press any key to release all handles." << STD_ENDL;
#ifdef GAF_DEBUG_MEMORY
// 			getchar();
#endif /*GAF_DEBUG_MEMORY*/
#endif /*GAF_DEBUG*/
			STD_COUT << "Clearing memory..." << STD_ENDL;
			GAF_MEM_RELEASE();
#ifdef GAF_DEBUG
			STD_COUT << "All memory release finish." << STD_ENDL;
#endif /*GAF_DEBUG*/
		}
		else
			STD_COUT << "The memory hasn't handles not release. " << STD_ENDL;
	}

	Application::Application()
	{
		m_isCreateWindow	= false;
		m_pMainWindow		= NULL;
		m_pMainScene		= NULL;
		m_isQuitApp		= false;
		
		if(ThePApp == NULL)
			ThePApp = this;
	}

	Application::~Application()
	{
		if(ThePApp == this)
			ThePApp = NULL;
	}

	bool Application::InitApp()
	{
		if(ThePApp == NULL)
		{
			ThePApp = this;
		}
		else if(ThePApp != this)
		{
			return false;
		}

		CreateWindow cw;
		Window::ResetCWStruct(cw);

		m_pMainScene = OnCreateApp(cw);

		if(m_pMainScene == NULL)
		{
			return false;
		}

		if(cw.pWindow != NULL)
			m_pMainWindow = cw.pWindow;
		else
		{
			m_pMainWindow = GAF_NEW(Window);
			m_isCreateWindow = true;
			if(m_pMainWindow->InitWindow(cw) == false)
			{
				return false;
			}
		}

		if(OnInitApp() == false)
			return false;

		return true;
	}

	int Application::RunApp()
	{
		if(PreRunApp())
		{
			m_pMainWindow->RunWindow();
		}

		return EndRunApp();
	}

	bool Application::PreRunApp()
	{
		if(m_pMainWindow == NULL)
		{
			return false;
		}

		if(!m_pMainWindow->ShowWindow())
		{
			return false;
		}

		if(!OnStartApp())
		{
			return false;
		}

		if(m_pMainScene == NULL)
		{
			return false;
		}

		m_pMainWindow->ShowScene(m_pMainScene);

		m_isQuitApp = false;
		return true;
	}

	int Application::EndRunApp()
	{
		m_isQuitApp = true;

		OnFinishApp();

		if(m_pMainWindow != NULL)
		{
			if(m_pMainWindow->GetTopScene() != NULL)
			{
				m_pMainWindow->ShowScene(NULL);
				m_pMainWindow->SwitchScene();
			}
			m_pMainWindow->HideWindow();

			if(m_isCreateWindow)
				GAF_DELETE(m_pMainWindow);
		}

		int nRtn = OnQuitApp();

		return nRtn;
	}

	void Application::UpdateApp()
	{
		// 更新
		OnUpdateApp();
	}

	void Application::QuitApp(bool isEnforce)
	{
		bool isQuitApp = true;

		// 询问程序是否可以退出
		if(isQuitApp)
		{
			isQuitApp = OnExitApp(isEnforce) || isEnforce;
		}

		// 询问场景是否可以退出
		if(isQuitApp && m_pMainWindow != NULL)
		{
			m_pMainWindow->ShowScene(NULL, isEnforce);
		}
	}

	void Application::DestroyApp()
	{
		OnDestroyApp();

		if(m_isCreateWindow && m_pMainWindow != NULL)
			GAF_DELETE(m_pMainWindow);
	}

	Window* Application::GetMainWindow()
	{
		return m_pMainWindow;
	}

	Scene* Application::GetMainScene()
	{
		return m_pMainScene;
	}

	bool Application::IsQuit()
	{
		return m_isQuitApp;
	}

	void Application::UpdateTimers()
	{
		static bool s_isUpdate = false;

		if(!s_isUpdate)
		{
			s_isUpdate = true;
			Timer::UpdateTimers();
			s_isUpdate = false;
		}
	}

	void Application::UpdateExactTimers()
	{
		static bool s_isUpdate = false;

		if(!s_isUpdate)
		{
			s_isUpdate = true;
			Timer::UpdateExactTimers();
			s_isUpdate = false;
		}
	}


	Timer::TimerList* Application::GetTimers()
	{
		return &m_lstTimers;
	}

	Timer::TimerList* Application::GetExactTimers()
	{
		return &m_lstExactTimers;
	}

}

